package com.ctgu.算法.最小生成树;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author: Backdiy
 * @data: 2021/4/16 19:14
 * @desc: 边多用prim，O(n*m)，点多用Kruskal，O(n+mlogm)
 */
/*数据
5 10
1 2 6
1 3 3
1 4 4
1 5 5
2 3 7
2 4 7
2 5 8
3 4 6
3 5 9
4 5 2
 */
public class 模板 {
    static class Prim {
        static int n;
        static int m;
        static int[][] cost;
        static int[] vis;
        static int[] min;
        static int ans = 0;

        public static void main(String[] args) throws IOException {
            init();
            solve();
        }

        static void init() throws IOException {
            n = IO.nextInt();
            m = IO.nextInt();
            cost = new int[n + 10][n + 10];
            vis = new int[n + 10];
            min = new int[n + 10];
            for (int i = 0; i < cost.length; i++) {
                Arrays.fill(cost[i], Integer.MAX_VALUE);
            }
            Arrays.fill(min, Integer.MAX_VALUE);
            for (int i = 0; i < m; i++) {
                int a = IO.nextInt();
                int b = IO.nextInt();
                int c = IO.nextInt();
                cost[a][b] = Math.min(cost[a][b],c);
                cost[b][a] = Math.min(cost[b][a],c);
            }
            min[1] = 0;
        }

        static void solve() {
            while (true) {
                int v = 0;
                for (int i = 1; i <= n; i++) {
                    if (vis[i] == 0 && (v == 0 || min[i] < min[v])) v = i;
                }
                System.out.println(v);
                if (v == 0) break;
                ans += min[v];
                vis[v] = 1;
                for (int i = 1; i <= n; i++) {
                    min[i] = Math.min(min[i], cost[v][i]);
                }
            }
            System.out.println(ans);
        }
    }

    static class Kruskal {

        static int n, m;
        static int par[];
        static int ans = 1;
        static int cost = 0;
        static ArrayList<Edge> edges = new ArrayList<>();

        public static void main(String[] args) throws IOException {
            init();
            solve();
        }

        static void unite(int x, int y) {
            int a = find(x);
            int b = find(y);
            if (a != b)
                par[a] = par[b];
        }

        static boolean same(int x, int y) {
            int a = find(x);
            int b = find(y);
            if (a == b)
                return true;
            return false;
        }

        static int find(int x) {
            return par[x] == x ? x : (par[x] = find(par[x]));
        }

        static void init() throws IOException {
            n = IO.nextInt();
            m = IO.nextInt();
            par = new int[n + 10];
            for (int i = 0; i < m; i++) {
                edges.add(new Edge(IO.nextInt(), IO.nextInt(), IO.nextInt()));
            }
            for (int i = 0; i < par.length; i++) {
                par[i] = i;
            }
        }

        static void solve() {
            edges.sort(Comparator.comparingInt(o -> o.v));
            for (int i = 0; i < edges.size(); i++) {
                System.out.println(i);
                if (ans == n) {
                    break;
                }
                if (!same(edges.get(i).a, edges.get(i).b)) {
                    unite(edges.get(i).a, edges.get(i).b);
                    cost += edges.get(i).v;
                    ans++;
                }
            }
            System.out.println(cost);
        }

        static class Edge {
            int a;
            int b;
            int v;

            public Edge(int a, int b, int v) {
                this.a = a;
                this.b = b;
                this.v = v;
            }
        }
    }

    static class IO {
        static StreamTokenizer streamTokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

        static String readLine() throws IOException {
            return bufferedReader.readLine();
        }

        static double nextDouble() throws IOException {
            streamTokenizer.nextToken();
            return (double) streamTokenizer.nval;
        }

        static int nextInt() throws IOException {
            streamTokenizer.nextToken();
            return (int) streamTokenizer.nval;
        }

        static long nextLongInt() throws IOException {
            streamTokenizer.nextToken();
            return (long) streamTokenizer.nval;
        }
    }
}
